package top.eggcode.component.query.util;

/**
 * Title:
 * Description: TODO
 * Date: 2021/9/20 9:34
 *
 * @author JiaQi Ding
 * @version 1.0
 */
public final class CaseFormat {

    /**
     * 转换成驼峰风格
     *
     * @param fieldName 字段名称
     * @return 字符串
     */
    public static String toCamel(String fieldName) {
        if (fieldName == null) {
            return null;
        }
        String[] split = fieldName.split("_");
        StringBuilder sb = new StringBuilder(fieldName.length());
        sb.append(split[0]);
        for (int i = 1; i < split.length; i++) {
            String element = split[i];
            char[] chars = element.toCharArray();
            if (chars[0] >= 'a' && chars[0] <= 'z') {
                chars[0] -= 32;
            }
            sb.append(chars);
        }
        return sb.toString();
    }

    /**
     * 转换成下划线风格
     *
     * @param name 字段名称
     * @return 字符串
     */
    public static String toUnderscore(String name) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < name.length(); ++i) {
            char ch = name.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                char chUcase = (char) (ch + 32);
                if (i > 0) {
                    buf.append('_');
                }
                buf.append(chUcase);
            } else {
                buf.append(ch);
            }
        }
        return buf.toString();
    }


}
